package cn.amebius.simpledocker.mapper;

import cn.amebius.simpledocker.controller.request.AddUserRequest;
import cn.amebius.simpledocker.entity.po.UserGroupRelationPo;
import cn.amebius.simpledocker.entity.po.UserPo;
import cn.amebius.simpledocker.entity.po.UserRoleRelationPo;
import cn.amebius.simpledocker.entity.dto.UserBaseInfoDto;
import cn.hutool.core.date.DateTime;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * tb_user的一些操作
 * @author 阿伟
 * @date 2023/7/27 19:46
 */
@Mapper
public interface UserMapper {

    @Select("select * from tb_user")
    List<UserPo> list();

    @Select("select * from tb_user limit #{start},#{pageSize}")
    List<UserPo> listByLimit( @Param("start") Integer start, @Param("pageSize") Integer pageSize);

    @Select("select count(1) from tb_user")
    Integer count();

    @Select("select * from tb_user where name like #{keyWords} limit #{start},#{pageSize}")
    List<UserPo> listByLimitLikeKeyWords(@Param("keyWords") String keyWords, @Param("start") Integer start, @Param("pageSize") Integer pageSize);

    @Select("select count(1) from tb_user where name like #{keyWords}")
    Integer countByLikeKeyWords(String keyWords);


    @Select("select * from tb_user where username = #{username}")
    UserPo selectByUsername(String username);

    @Select("select * from tb_user where id = #{id}")
    UserPo selectById(Integer id);

    @Update("update tb_user set password = #{newPassword} where id = #{id}")
    Integer updatePasById(@Param("newPassword") String newPassword,@Param("id") Integer id);


    @Select("select * from tb_user where id = #{id}")
    UserBaseInfoDto selectInfoById(Integer id);

    @Select("select * from tb_user_role_relation where user_id = #{userId}")
    UserRoleRelationPo selectUserRoleByUserId(Integer userId);

    @Select("select * from tb_user_group_relation where user_id = #{userId}")
    UserGroupRelationPo selectUserGroupByUserId(Integer userId);

    @Insert("insert into tb_user " +
            "(username,password,name,email,mobile,create_time,deleted) " +
            "values " +
            "(#{aur.username},#{aur.password},#{aur.name},#{aur.email},#{aur.mobile},#{createTime},0)")
    Integer insertUser(@Param("aur") AddUserRequest aur, @Param("createTime") DateTime createTime);

    @Insert("insert into tb_user_group_relation " +
            "(user_id,group_id,create_time,deleted) " +
            "values " +
            "(#{userId},#{groupId},#{createTime},0)")
    Integer insertUserAndGroup(@Param("userId") Integer userId,@Param("groupId") Integer groupId,@Param("creatTime") DateTime createTime);

    @Insert("insert into tb_user_role_relation " +
            "(user_id,role_id,create_time,deleted) " +
            "values " +
            "(#{userId},#{roleId},#{createTime},0)")
    Integer insertUserAndRole(@Param("userId") Integer userId,@Param("roleId") Integer roleId,@Param("creatTime") DateTime createTime);
}
